layui.define(['jquery', 'form'], function(exports) {
    "use strict";
    var $ = layui.jquery, form = layui.form;
    var obj = {
        assign: function(d) {
            var o = '';
            for (var i in d) {
                switch($('.field-'+i).attr('type')) {
                    case 'select':
                        o = $('.field-'+i).find('option[value="'+d[i]+'"]');
                        o.prop("selected", true);
                        break;

                    case 'radio':
                        o = $('.field-'+i+'[value="'+d[i]+'"]');
                        o.prop('checked', true);
                        break;

                    case 'checkbox':
                        if (typeof(d[i]) == 'object') {
                            for(var j in d[i]) {
                                o = $('.field-'+i+'[value="'+d[i][j]+'"]');
                                o.prop('checked', true);
                            }
                        } else {
                            o = $('.field-'+i+'[value="'+d[i]+'"]');
                            o.prop('checked', true);
                        }
                        break;

                    case 'img':
                        if (d[i]) {
                            o = $('.field-'+i);
                            o.attr('src', d[i]);
                        }
                        break;

                    default:
                        o = $('.field-'+i);
                        o.val(d[i]);
                        break;
                }

                if (o.attr('data-disabled')) {
                    o.prop('disabled', true);
                }

                if (o.attr('data-readonly')) {
                    o.prop('readonly', true);
                }
            }
            form.render();
        },
        linkage: {
            url: '',
            elem: '',
            name: '',
            placeholder: '请选择...',
            value: '',
            lastValue: true,
            lastShow: false,
            init: function(c) {
                var c = Object.assign({}, this, c);
                $(c.elem).html('<button type="button" class="layui-input"><span>'+c.placeholder+'</span><i class="layui-icon layui-icon-triangle-d"></i></button><div class="hisi-linkage-pop"></div><input type="hidden" name="'+c.name+'" value="'+c.value+'" />');
                $(document).on('click', c.elem+' button', function(e) {
                    e.stopPropagation();
                    var obj = $(c.elem);
                    $.get(c.url, {}, function(data) {
                        var html = '<ul>';
                        for(var i in data) {
                            html += '<li class="linkage-item" data-id="' + data[i].id + '">'+ data[i].name +'</li>';
                        }
                        html += '</ul>';
                        obj.children('.hisi-linkage-pop').html(html).show();
                        var ulObj = obj.children('.hisi-linkage-pop').children('ul');
                        /* 监听子菜单点击事件 */
                        $(c.elem).on('click', '.linkage-item', function(e) {
                            e.stopPropagation();
                            var that = $(this),
                                index = that.parent('ul').index(),
                                popObj = that.parents('.hisi-linkage-pop'),
                                str = '',
                                chooseId = '',
                                chooseIds = [],
                                chooseLast = true;
                            that.addClass('active').siblings('li').removeClass('active');
                            popObj.find('.active').each(function(i) {
                                if (c.lastShow) {
                                    str = $(this).text();
                                } else {
                                    if (i > 0) str += ' / ';
                                    str += $(this).text();
                                }

                                chooseId = $(this).attr('data-id');
                                chooseIds.push(chooseId);
                            });

                            obj.find('span').html(str);

                            $.get(c.url, {pid: that.attr('data-id')}, function(data) {
                                if (data.length <= 0) {
                                    popObj.children('ul').each(function(i) {
                                        if (!$(this).children('.active')[0]) {
                                            chooseLast = false;
                                        }
                                    });

                                    if (chooseLast) {
                                        if (c.lastValue) {
                                            obj.children('input').val(chooseId);
                                        } else {
                                            obj.children('input').val(chooseIds.join(','));
                                        }
                                    }
                                    popObj.hide();
                                    chooseId = '';
                                    chooseIds = [];
                                    return;
                                }

                                popObj.find('ul').each(function(i) {
                                    if (i > index) $(this).remove();
                                });

                                var html = '<ul>';
                                for(var i in data) {
                                    html += '<li class="linkage-item" data-id="' + data[i].id + '">'+ data[i].name +'</li>';
                                }
                                html += '</ul>';
                                popObj.append(html);
                            }, 'json');

                            $(document).on('click', function() {
                                obj.children('.hisi-linkage-pop').html('').hide();
                            });
                        });
                    }, 'json');
                });
            }
        },
        inputTag: {
            def: [],
            elem: '.hisi-input-tag',
            done: '',
            split: ' ',
            bgc: '',
            init: function(c) {
                var t = Object.assign({}, this, c);
                $('.hisi-input-tags .input').each(function() {
                    var that = $(this),
                        v = that.val(),
                        split = that.attr('data-split') || t.split,
                        bgc = that.attr('data-background') || t.bgc;
                    if (v) {
                        var a = v.trim().split(split), h = '';
                        for(var i in a) {
                            h += '<span style="background-color:'+bgc+'"><em>'+a[i]+'</em><i class="hisi-input-tag-del">x</i></span>';
                        }
                        that.before(h);
                    }
                });

                $('.hisi-input-tags').on('click', function() {
                    $(this).children('.hisi-input-tag').focus();
                });

                $(t.elem).keypress(function(e) {
                    if (e.which == 13) {
                        var that = $(this),
                            i = that.siblings('.input'),
                            s = i.attr('data-split') || t.split,
                            b = i.attr('data-bgc') || t.bgc,
                            v = that.val().replace(s, ''),
                            a = i.val().trim().split(s);
                        that.val('').focus();
                        if ($.inArray(v, a) != -1) return false;
                        that.before('<span style="background-color:'+b+'"><em>'+v+'</em><i class="hisi-input-tag-del">x</i></span>');
                        if (i.val()) {
                            i.val(i.val()+s+v);
                        } else {
                            i.val(v);
                        }
                        t.done && typeof t.done === 'function' && t.done(v);
                        return false;
                    }
                });

                $(document).on('click', '.hisi-input-tag-del', function(){
                    var that = $(this),
                        txt = that.siblings('em').text(),
                        i = that.parents('.hisi-input-tags').children('.input'),
                        s = i.attr('data-split') || t.split,
                        a = i.val().trim().split(s);
                    a.splice($.inArray(txt, a), 1);
                    i.val(a.join(s));
                    that.parent('span').remove();
                });
            }
        }
    };

    exports('func', obj);
}); 